package com.sinosun.autotestmachine.tools.proxy;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sinosun.autotestmachine.App;
import com.sinosun.autotestmachine.tools.ValueInfo;
import com.sinosun.autotestmachine.tools.ValueType;
import com.sinosun.autotestmachine.tools.Collector.Cache;
import com.sinosun.autotestmachine.tools.parser.GroupHolder;
import com.sinosun.autotestmachine.tools.parser.ParamKey;
import com.sinosun.autotestmachine.tools.utils.Utils;
import com.sinosun.mstplib.MstpClient;
import com.sinosun.mstplib.MstpException;
import com.sinosun.mstplib.group.AuthMode;
import com.sinosun.mstplib.group.Group;
import com.sinosun.mstplib.group.GroupMember;
import com.sinosun.mstplib.group.GroupMemberRole;
import com.sinosun.mstplib.group.GroupResult;
import com.sinosun.mstplib.group.GroupType;
import com.sinosun.mstplib.group.InviteMode;

import android.text.TextUtils;
import android.util.Log;

public class GroupModule {
	
	/**
	 * 将JSONString 转换成对应的参数实体
	 * @param param
	 * @return
	 */
	private static GroupHolder getGroupParam(String param) {
		GroupHolder ret = new GroupHolder();
		try {
			JSONObject jo = new JSONObject(param);
			if (jo.has(ParamKey.DESC)) {
				ret.setDescription(jo.getString(ParamKey.DESC));
			}
			if (jo.has(ParamKey.GROUP_NAME)) {
				ret.setGroupName(jo.getString(ParamKey.GROUP_NAME));
			}
			if (jo.has(ParamKey.GROUP_DESC)) {
				ret.setGroupDesc(jo.getString(ParamKey.GROUP_DESC));
			}
			if (jo.has(ParamKey.GROUP_TYPE)) {
				int tag = 0;
				if (jo.getInt(ParamKey.GROUP_TYPE) == 0) {
					tag = 1;
				} else if (jo.getInt(ParamKey.GROUP_TYPE) == 1) {
					tag = 0;
				}
				ret.setGroupType(tag);
			}
			if (jo.has(ParamKey.AUTH_MODE)) {
				ret.setAuthMode(jo.getInt(ParamKey.AUTH_MODE));
			}
			if (jo.has(ParamKey.INVITE_MODE)) {
				ret.setInviteMode(jo.getInt(ParamKey.INVITE_MODE));
			}
			if (jo.has(ParamKey.MEMBER_LIST)) {
				JSONArray ja = jo.getJSONArray(ParamKey.MEMBER_LIST);
				List<String> all = new ArrayList<String>();
				for (int i = 0; i < ja.length(); i++) {
					ValueInfo value = Utils.getUserRealInfo(ja.getString(i));
					String mstpId = value.getValueContent();
					if (value.getValueType() == ValueType.INDEX) {		// value 数据内容符合匹配规则，从内存中取出对应的 mstpId
						mstpId = App.getTestConfig().getUsers().get(value.getValueContent());
						if (TextUtils.isEmpty(mstpId)) {
							return null;
						}
					}
					all.add(mstpId);
				}
				ret.setMemberList(all);
			}
			
			if (jo.has(ParamKey.EXT_INFO)) {
				ret.setExtInfo(jo.getString(ParamKey.EXT_INFO));
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return ret;
	}

	/**
	 * 同步创建群组
	 * @param mstpClient
	 * @param paramJSON
	 * @param cacheParam
	 * @return
	 */
	protected static int createGroup(MstpClient mstpClient, String paramJSON, String cacheParam) {		//创建群组
		List<String> params = new ArrayList<String>();
		List<String> caches = new ArrayList<String>();
		try {
			JSONObject joParams = new JSONObject(paramJSON);
			
			if (joParams.has(ParamKey.GROUP_PARAM)) {		//获取所有群组实际参数情况
				JSONArray ja = joParams.getJSONArray(ParamKey.GROUP_PARAM);
				for (int i = 0; i < ja.length(); i++) {
					ValueInfo vi = Utils.getGroupRealInfo(ja.getString(i));
					String tmp = new String("");
					if (vi.getValueType() == ValueType.INDEX) {
						tmp = App.getTestConfig().getGroupContent().get(vi.getValueContent());
					} else {
						tmp = App.getTestConfig().getGroupContent().get(ja.getString(i));
					}
					params.add(tmp);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			if (cacheParam != null) {
				JSONObject joCaches = new JSONObject(cacheParam);
				if (joCaches.has(ParamKey.GROUP_ID)) {			//获取所有缓存信息
					JSONArray ja = joCaches.getJSONArray(ParamKey.GROUP_ID);
					for (int i = 0; i < ja.length(); i++) {
						caches.add(ja.getString(i));
					}
				} else {
					return ErrorCode.ERR_PARAM_ERROR;
				}
			}
			
		} catch (JSONException e1) {
			e1.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		for (int i = 0; i < params.size(); i++) {		//遍历所有的群组参数信息，开始创建群组
			String param = params.get(i);
			GroupHolder groupInfo = getGroupParam(param);
			if (groupInfo == null) {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			List<GroupMember> groupMembers = new ArrayList<GroupMember>();
			for (String ele : groupInfo.getMemberList()) {
				GroupMember item = new GroupMember(ele);
				item.setRole(GroupMemberRole.COMMON);
				groupMembers.add(item);
			}
			try {
				GroupResult groupResult = mstpClient.getGroupManager().createGroup(groupInfo.getGroupName(), groupInfo.getGroupDesc(), GroupType.valueOf(groupInfo.getGroupType()), 
						AuthMode.valueOf(groupInfo.getAuthMode()), InviteMode.valueOf(groupInfo.getInviteMode()), groupMembers, groupInfo.getExtInfo());
				
				if (groupResult != null && compareGroup(mstpClient, groupResult.getGroupMstpId(), groupInfo)) {	//存在群组 mstpId认为群组创建成功
					groupInfo.setMstpId(groupResult.getGroupMstpId());
					Cache.groupInfoCache.put(caches.get(i), groupInfo);
				} else {		//只要有一个群组创建失败，认为接口调用失败
					return ErrorCode.ERR_UNEXPECT_RESULT;
				}
				try {
					mstpClient.getCloudDiskManager().createAccount(groupResult.getGroupMstpId());
				} catch (MstpException e) {
					e.printStackTrace();
				}
			} catch (MstpException e) {
				e.printStackTrace();
				return ErrorCode.ERR_UNEXPECT_RESULT;
			}
		}
		return ErrorCode.SUCCESS;
	}
	
	private static boolean compareGroup(MstpClient mstpClient, String groupId, GroupHolder groupInfo) {
		try {
			Group group = mstpClient.getGroupManager().queryGroupInfo(groupId);
			if (group == null) {
				return false;
			}
			
			if (group.getAuthMode() == AuthMode.valueOf(groupInfo.getAuthMode()) && group.getGroupName().equals(groupInfo.getGroupName()) && 
				group.getGroupDesc().equals(groupInfo.getGroupDesc()) && group.getGroupType() == GroupType.valueOf(groupInfo.getGroupType()) &&
				group.getInviteMode() == InviteMode.valueOf(groupInfo.getInviteMode())) {
				return true;
			} else {
				return false;
			}
		} catch (MstpException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 同步解散群组
	 * @param mstpClient
	 * @param paramJson
	 * @return
	 */
	protected static int dismissGroup(MstpClient mstpClient, String paramJson) {
		String groupId = new String("");
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.GROUP_ID)) {
				if (jo.isNull(ParamKey.GROUP_ID)) {
					groupId = null;
				} else {
					groupId = jo.getString(ParamKey.GROUP_ID);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		if (groupId != null) {
			ValueInfo vi = Utils.getGroupMstpInfo(groupId);
			if (vi.getValueType() == ValueType.INDEX) {
				GroupHolder groupResult = Cache.groupInfoCache.get(groupId);
				groupId = groupResult.getMstpId();
			}
		}
		int ret = ErrorCode.SUCCESS;
		try {
			ret = mstpClient.getGroupManager().dismissGroup(groupId, null);
		} catch (MstpException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		}
		return ret;
	}
	
	/**
	 * 同步的加入群组
	 * @param mstpClient
	 * @param paramJson
	 * @return
	 */
	protected static int joinGroup(MstpClient mstpClient, String paramJson) {
		String groupId = new String("");
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.GROUP_ID)) {
				if (jo.isNull(ParamKey.GROUP_ID)) {
					groupId = null;
				} else {
					groupId = jo.getString(ParamKey.GROUP_ID);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		if (groupId != null) {
			ValueInfo vi = Utils.getGroupMstpInfo(groupId);
			if (vi.getValueType() == ValueType.INDEX) {
				GroupHolder groupResult = Cache.groupInfoCache.get(groupId);
				groupId = groupResult.getMstpId();
			}
		}
		
		int ret = ErrorCode.SUCCESS;
		try {
			ret = mstpClient.getGroupManager().joinGroup(groupId, null);
		} catch (MstpException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		}
		return ret;
	}
	
	/**
	 * 验证指定群组中是否存在指定的用户
	 * @param mstpClient
	 * @param paramJson
	 * @return
	 */
	protected static int verifyUserInGroup(MstpClient mstpClient, String paramJson) {
		String groupId = new String("");
		List<String> mstpIDs = new ArrayList<String>();
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.GROUP_ID)) {
				if (jo.isNull(ParamKey.GROUP_ID)) {
					groupId = null;
				} else {
					groupId = jo.getString(ParamKey.GROUP_ID);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			if (jo.has(ParamKey.MSTPID)) {
				JSONArray mstpIdJa = jo.getJSONArray(ParamKey.MSTPID);
				for (int i = 0; i < mstpIdJa.length(); i++) {
					String tmp = mstpIdJa.getString(i);
					
					ValueInfo value = Utils.getUserRealInfo(tmp);
					tmp = value.getValueContent();
					if (value.getValueType() == ValueType.INDEX) {		// value 数据内容符合匹配规则，从内存中取出对应的 mstpId
						tmp = App.getTestConfig().getUsers().get(value.getValueContent());
					}
					if (TextUtils.isEmpty(tmp)) {
						return ErrorCode.ERR_PARAM_ERROR;
					}
					mstpIDs.add(tmp);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		if (groupId != null) {
			GroupHolder groupResult = Cache.groupInfoCache.get(groupId);
			groupId = groupResult.getMstpId();
		}
		int ret = ErrorCode.SUCCESS;
		try {
			testGroup(mstpClient, groupId);
			
			List<GroupMember> querryResult = mstpClient.getGroupManager().queryGroupMember(groupId);
			if (querryResult == null) {
				return ErrorCode.ERR_USERINGROUP_ERROR;
			}
			
			for (GroupMember member : querryResult) {
				Log.e("XXX", "member -> " + member.getMember());
			}
			List<String> allMember = new ArrayList<String>();
			for (GroupMember item : querryResult) {
				allMember.add(item.getMember());
			}
			if (allMember.containsAll(mstpIDs)) {
				ret = ErrorCode.SUCCESS;
			} else {
				ret = ErrorCode.ERR_USERINGROUP_ERROR;
			}
		} catch (MstpException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		}
		
		return ret;
	}
	
	private static void testGroup(MstpClient mstpClient, String groupId) {
		try {
			Group testGroup = mstpClient.getGroupManager().queryGroupInfo(groupId);
			Log.e("XXX", testGroup.getGroupMstpId());
			Log.e("XXX", testGroup.getCreator());
			Log.e("XXX", testGroup.getGroupDesc());
			Log.e("XXX", "" + testGroup.getAuthMode());
			Log.e("XXX", "" + testGroup.getGroupType());
		} catch (MstpException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 离开群组
	 * @param mstpClient
	 * @param paramJson
	 * @return
	 */
	protected static int leaveGroup(MstpClient mstpClient, String paramJson) {
		String groupId = new String("");
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.GROUP_ID)) {
				if (jo.isNull(ParamKey.GROUP_ID)) {
					groupId = null;
				} else {
					groupId = jo.getString(ParamKey.GROUP_ID);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		if (groupId != null) {
			ValueInfo vi = Utils.getGroupMstpInfo(groupId);
			if (vi.getValueType() == ValueType.INDEX) {
				GroupHolder groupResult = Cache.groupInfoCache.get(groupId);
				groupId = groupResult.getMstpId();
			}
		}
		
		int ret = ErrorCode.SUCCESS;
		try {
			ret = mstpClient.getGroupManager().leaveGroup(groupId, null);
		} catch (MstpException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		}
		
		return ret;
	}
	
	/**
	 * 移除群组成员
	 * @param mstpClient
	 * @param paramJson
	 * @return
	 */
	protected static int removeGroupMember(MstpClient mstpClient, String paramJson) {
		String groupId = new String("");
		List<String> mstpIDs = new ArrayList<String>();
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.GROUP_ID)) {
				if (jo.isNull(ParamKey.GROUP_ID)) {
					groupId = null;
				} else {
					groupId = jo.getString(ParamKey.GROUP_ID);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			if (jo.has(ParamKey.MEMBER_LIST)) {
				JSONArray mstpIdJa = jo.getJSONArray(ParamKey.MEMBER_LIST);
				for (int i = 0; i < mstpIdJa.length(); i++) {
					String tmp = mstpIdJa.getString(i);
					
					ValueInfo value = Utils.getUserRealInfo(tmp);
					tmp = value.getValueContent();
					if (value.getValueType() == ValueType.INDEX) {		// value 数据内容符合匹配规则，从内存中取出对应的 mstpId
						tmp = App.getTestConfig().getUsers().get(value.getValueContent());
					}
					if (TextUtils.isEmpty(tmp)) {
						return ErrorCode.ERR_PARAM_ERROR;
					}
					mstpIDs.add(tmp);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		if (groupId != null) {
			ValueInfo vi = Utils.getGroupMstpInfo(groupId);
			if (vi.getValueType() == ValueType.INDEX) {
				GroupHolder groupResult = Cache.groupInfoCache.get(groupId);
				groupId = groupResult.getMstpId();
			}
		}
		
		int ret = ErrorCode.SUCCESS;
		try {
			GroupResult result = mstpClient.getGroupManager().removeMember(groupId, mstpIDs, null);
			if (result == null) {
				return ErrorCode.ERR_REMOVE_GROUPMEMBER_ERROR;
			}
			
			if (result.getSuccessList().containsAll(mstpIDs)) {
				ret = ErrorCode.SUCCESS;
			} else {
				ret = ErrorCode.ERR_REMOVE_GROUPMEMBER_ERROR;
			}
		} catch (MstpException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		}
		return ret;
	}
	
	protected static int queryGroupInfo(MstpClient mstpClient, String paramJson) {
		String groupId = new String("");
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.GROUP_ID)) {
				if (jo.isNull(ParamKey.GROUP_ID)) {
					groupId = null;
				} else {
					groupId = jo.getString(ParamKey.GROUP_ID);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		Log.e("XXX", "queryGroupInfo -> groupId=" + groupId);
		if (groupId.equals("null")) {
			groupId = null;
		}
		if (groupId != null) {
			ValueInfo vi = Utils.getGroupMstpInfo(groupId);
			if (vi.getValueType() == ValueType.INDEX) {
				GroupHolder groupResult = Cache.groupInfoCache.get(groupId);
				groupId = groupResult.getMstpId();
			}
		}
		
		int ret = ErrorCode.SUCCESS;
		try {
			Group group = mstpClient.getGroupManager().queryGroupInfo(groupId);
			if (group != null) {
				ret = ErrorCode.SUCCESS;
			} else {
				ret = ErrorCode.ERR_QUERY_GROUPINFO_ERROR;
			}
		} catch (MstpException e) {
			e.printStackTrace();
			ret = e.getErrorCode();
		}
		return ret;
	}
	
	protected static int inviteGroupMember(MstpClient mstpClient, String paramJson) {
		String groupId = new String("");
		List<String> members = new ArrayList<String>();
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.GROUP_ID)) {
				if (jo.isNull(ParamKey.GROUP_ID)) {
					groupId = null;
				} else {
					groupId = jo.getString(ParamKey.GROUP_ID);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			if (jo.has(ParamKey.INVITEE_LIST)) {
				JSONArray mstpIdJa = jo.getJSONArray(ParamKey.INVITEE_LIST);
				for (int i = 0; i < mstpIdJa.length(); i++) {
					String tmp = mstpIdJa.getString(i);
					
					ValueInfo value = Utils.getUserRealInfo(tmp);
					tmp = value.getValueContent();
					if (value.getValueType() == ValueType.INDEX) {		// value 数据内容符合匹配规则，从内存中取出对应的 mstpId
						tmp = App.getTestConfig().getUsers().get(value.getValueContent());
					}
					if (TextUtils.isEmpty(tmp)) {
						return ErrorCode.ERR_PARAM_ERROR;
					}
					members.add(tmp);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		if (groupId != null) {
			ValueInfo vi = Utils.getGroupMstpInfo(groupId);
			if (vi.getValueType() == ValueType.INDEX) {
				GroupHolder groupResult = Cache.groupInfoCache.get(groupId);
				groupId = groupResult.getMstpId();
			}
		}
		
		int ret = ErrorCode.SUCCESS;
		try {
			GroupResult gr = mstpClient.getGroupManager().inviteMember(groupId, members, null);
			if (gr.getSuccessList().containsAll(members)) {
				ret = ErrorCode.SUCCESS;
			} else {
				ret = ErrorCode.ERR_UNEXPECT_RESULT;
			}
		} catch (MstpException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		}
		return ret;
	}
	
	protected static int setMemberNickname(MstpClient mstpClient, String paramJson) {
		String groupId = new String("");
		String mstpId = new String("");
		String nickName = new String("");
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.GROUP_ID)) {
				if (jo.isNull(ParamKey.GROUP_ID)) {
					groupId = null;
				} else {
					groupId = jo.getString(ParamKey.GROUP_ID);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			if (jo.has(ParamKey.TARGET)) {
				mstpId = jo.getString(ParamKey.TARGET);
				ValueInfo value = Utils.getUserRealInfo(mstpId);
				mstpId = value.getValueContent();
				if (value.getValueType() == ValueType.INDEX) {
					mstpId = App.getTestConfig().getUsers().get(value.getValueContent());
					if (TextUtils.isEmpty(mstpId)) {
						return ErrorCode.ERR_PARAM_ERROR;
					}
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			if (jo.has(ParamKey.NICK_NAME)) {
				nickName = jo.getString(ParamKey.NICK_NAME);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		if (groupId != null) {
			ValueInfo vi = Utils.getGroupMstpInfo(groupId);
			if (vi.getValueType() == ValueType.INDEX) {
				GroupHolder groupResult = Cache.groupInfoCache.get(groupId);
				groupId = groupResult.getMstpId();
			}
		}
		
		int ret = ErrorCode.SUCCESS;
		try {
			ret = mstpClient.getGroupManager().setMemberNickname(groupId, mstpId, nickName);
			
			if (ret != ErrorCode.SUCCESS) {
				return ret;
			}
			
			List<GroupMember> groupMembers = mstpClient.getGroupManager().queryGroupMember(groupId);
			for (GroupMember member : groupMembers) {
				if (member.getMember().equals(mstpId) && member.getNickName().equals(nickName)) {
					return ErrorCode.SUCCESS;
				}
			}
			return ErrorCode.ERR_UNEXPECT_RESULT;
		} catch (MstpException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		}
		return ret;
	}
	
	protected static int queryGroupMember(MstpClient mstpClient, String paramJson) {
		String groupId = new String("");
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.GROUP_ID)) {
				if (jo.isNull(ParamKey.GROUP_ID)) {
					groupId = null;
				} else {
					groupId = jo.getString(ParamKey.GROUP_ID);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		if (groupId != null) {
			ValueInfo vi = Utils.getGroupMstpInfo(groupId);
			if (vi.getValueType() == ValueType.INDEX) {
				GroupHolder groupResult = Cache.groupInfoCache.get(groupId);
				groupId = groupResult.getMstpId();
			}
		}
		
		int ret = ErrorCode.SUCCESS;
		try {
			List<GroupMember> allMember = mstpClient.getGroupManager().queryGroupMember(groupId);
			if (allMember == null) {
				ret = ErrorCode.ERR_QUERY_GROUPMEMBER_ERROR;
			} else {
				ret = ErrorCode.SUCCESS;
			}
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		}
		return ret;
	}
	
	protected static int updateGroupInfo(MstpClient mstpClient, String paramJson) {
		String groupId = new String("");
		GroupHolder groupInfo = null;
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.GROUP_ID)) {
				if (jo.isNull(ParamKey.GROUP_ID)) {
					groupId = null;
				} else {
					groupId = jo.getString(ParamKey.GROUP_ID);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			String tmp = new String("");
			if (jo.has(ParamKey.UPDATE_GROUP_INFO)) {
				tmp = jo.getString(ParamKey.UPDATE_GROUP_INFO);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			ValueInfo vi = Utils.getGroupRealInfo(tmp);
			if (vi.getValueType() == ValueType.INDEX) {
				tmp = App.getTestConfig().getGroupContent().get(vi.getValueContent());
			}
			if (TextUtils.isEmpty(tmp)) {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			groupInfo = getGroupParam(tmp);
			
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		if (groupId != null) {
			ValueInfo vi = Utils.getGroupMstpInfo(groupId);
			if (vi.getValueType() == ValueType.INDEX) {
				GroupHolder groupResult = Cache.groupInfoCache.get(groupId);
				groupId = groupResult.getMstpId();
			}
		}
		
		int ret = ErrorCode.SUCCESS;
		try {
			Log.e("XXX", "updateGroupInfo step mstpId=" + groupInfo.getMstpId());
			Log.e("XXX", "updateGroupInfo step groupName=" + groupInfo.getGroupName());
			Log.e("XXX", "updateGroupInfo step groupDesc=" + groupInfo.getGroupDesc());
			Log.e("XXX", "updateGroupInfo step authMode=" + AuthMode.valueOf(groupInfo.getAuthMode()));
			Log.e("XXX", "updateGroupInfo step inviteMode=" + InviteMode.valueOf(groupInfo.getInviteMode()));
			ret = mstpClient.getGroupManager().setGroupInfo(groupId, groupInfo.getGroupName(), groupInfo.getGroupDesc(), 
					AuthMode.valueOf(groupInfo.getAuthMode()), InviteMode.valueOf(groupInfo.getInviteMode()));
			if (ret != ErrorCode.SUCCESS) {
				return ret;
			}
			
			if(compareGroup(mstpClient, groupId, groupInfo)) {
				return ErrorCode.SUCCESS;
			} else {
				return ErrorCode.ERR_UNEXPECT_RESULT;
			}
		} catch (MstpException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		}
		return ret;
	}
}
